/**************************************************************************
 * @file BC_QC_PHY.h
 * @author BryanHe(贺璧) (532352406@qq.com)
 * @brief
 * @version 0.1
 * @date 2025-08-18
 *
 * @copyright Copyright (c) 2025
 *
 *************************************************************************/
#ifndef BC_QC_PHY__H_H
#define BC_QC_PHY__H_H

#ifdef __cplusplus
extern "C" {
#endif

#include "CH543.h"
#include "DEBUG.H"
#include "ADC.H"

#define Volt0_1 124u
#define Volt0_3 372u
#define Volt0_4 497u
#define Volt0_6 745u


#define DMAisle  CH9
#define DPAisle  CH8
#define VBUS_CH  CH10
#define DC_IN_CH CH11
//5V*0.07578558225508317929759704251386
//9V*0.07578558225508317929759704251386
//12V*0.07578558225508317929759704251386
//20V*0.07578558225508317929759704251386
#define VBUS_4V    376u
#define VBUS_8V    752u
#define VBUS_11V   1034u
#define VBUS_18P5V 1740u
typedef enum {
    BC_TYPE_NULL = 0,
    BC_TYPE_CDP,  // 充电下游端口CDP
    BC_TYPE_SDP,  // 标准下游端口SDP
    BC_TYPE_DCP,  // 专用充电端口DCP
    
    BC_T_CDP_OR_DCP,
    BC_TYPE_UN_KNOWN,
} BC_TYPE_T;
#define SET_DM_Push_Pull() \
    {                      \
        P3_MOD_OC &= ~bDM; \
        P3_DIR_PU |= bDM;  \
    }
#define SET_DP_Push_Pull() \
    {                      \
        P3_MOD_OC &= ~bDP; \
        P3_DIR_PU |= bDP;  \
    }
#define SET_DM_Float_in()  \
    {                      \
        P3_MOD_OC &= ~bDM; \
        P3_DIR_PU &= ~bDM; \
    }
#define SET_DP_Float_in()  \
    {                      \
        P3_MOD_OC &= ~bDP; \
        P3_DIR_PU &= ~bDP; \
    }
#define SET_DMDP_Float_in() \
    {                       \
        SET_DM_Float_in();  \
        SET_DP_Float_in();  \
    }
#define USB_DP_OUT_0v6()                         \
    {                                            \
        USB_PULL &= ~bUDP_PDE;                   \
        USB_PULL |= bUDM_PDE;                    \
        P3_ANA_PIN |= (MASK_USB_OUT_VOL & 0x01); \
    }
#define USB_DM_OUT_0v6()                         \
    {                                            \
        USB_PULL |= bUDP_PDE;                    \
        USB_PULL &= ~bUDM_PDE;                   \
        P3_ANA_PIN |= (MASK_USB_OUT_VOL & 0x01); \
    }
#define USB_DP_OUT_0v0()                   \
    {                                      \
        USB_PULL |= bUDP_PDE;              \
        USB_PULL &= ~bUDM_PDE;             \
        P3_ANA_PIN &= ~(MASK_USB_OUT_VOL); \
    }
#define USB_DM_OUT_0v0()                   \
    {                                      \
        USB_PULL &= ~bUDP_PDE;             \
        USB_PULL |= bUDM_PDE;              \
        P3_ANA_PIN &= ~(MASK_USB_OUT_VOL); \
    }
#define USB_DP_OUT_3v3()    \
    {                       \
        SET_DP_Push_Pull(); \
        SET_DM_Float_in();  \
        DP = 1;             \
    }
#define USB_DM_OUT_3v3()    \
    {                       \
        SET_DM_Push_Pull(); \
        SET_DP_Float_in();  \
        DM = 1;             \
    }

#define QC_Get_Request5V()   \
    {                        \
        SET_DMDP_Float_in(); \
        USB_DM_OUT_0v0();    \
        USB_DP_OUT_0v6();    \
    }
#define QC_Get_Request9V()   \
    {                        \
        SET_DMDP_Float_in(); \
        USB_DM_OUT_0v6();    \
        USB_DP_OUT_3v3();    \
    }
#define QC_Get_Request12V()                      \
    {                                            \
        SET_DMDP_Float_in();                     \
        USB_PULL |= bUDP_PDE;                    \
        USB_PULL |= bUDM_PDE;                    \
        P3_ANA_PIN |= (MASK_USB_OUT_VOL & 0x01); \
    }
#define QC_Get_Request20V() \
    {                       \
        USB_DM_OUT_3v3();   \
        USB_DP_OUT_3v3();   \
    }

#define QC3_Init()           \
    {                        \
        SET_DMDP_Float_in(); \
        USB_DP_OUT_0v6();    \
        USB_DM_OUT_3v3();    \
    }
#define QC3_Plus_Volt()     \
    {                       \
        SET_DP_Push_Pull(); \
        DP = 1;             \
        mDelaymS(10);       \
        QC3_Init();         \
    }
#define QC3_SUB_Volt()       \
    {                        \
        QC_Get_Request12V(); \
        mDelaymS(10);        \
        QC3_Init();          \
    }
BC_TYPE_T BC1_2Check(void);
UINT16 Check_ADC(UINT8 ch);

#ifdef __cplusplus
}
#endif

#endif  // BC_QC_PHY__H_H
